package main

import (
	"fmt"
	"net"
	"net/http"
	"net/rpc"
	"study.com/RPCandgRPC/rpc/model"
)

//type userGender int
//var Genders= []string{"男","女"}
//const(
//	Male userGender = iota
//	Female
//)
//
//type User struct {
//	Name   string
//	Age    int
//	Gender userGender
//}
type Person struct {

}
//func (u *Person) GetUser(args models.User, user *string) error {
//	*user = "姓名：" + args.Name + "，" + "年龄："+ strconv.Itoa(args.Age) + "，" + "性别：" + models.Genders[args.Gender]
//	return nil
//}

//向外暴露接口
func (u *Person) GetUser(args model.User, user *model.User) error {
	*user = model.User{Name: args.Name, Age: args.Age, Gender: args.Gender}
	return nil
}

func main() {
	//初始化为指针类型
	person := new(Person)
	//将服务器对象注册
	err := rpc.Register(person)
	//可自定义服务名称，Register自动将服务名称设为Person
	//err := rpc.RegisterName("Person",person)
	if err!=nil{
		//panic(err.Error())
		fmt.Println("rpc.Register错误=",err)
		//return
	}
	//将user中提供的服务器注册到HTTP协议中，调用者可以利用http的方式进行数据传递
	rpc.HandleHTTP()

	//监听
	listen, err := net.Listen("tcp", ":9000")
	if err!=nil{
		fmt.Println("net.Listen error=", err)
		//return
	}
	// 开启协程服务
	//go http.Serve(listen, nil)
	http.Serve(listen, nil)

}


